{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['/home/HwHiAiUser/Ascend', '/home/HwHiAiUser/mhc/paintfix_python', '/usr/lib/python36.zip', '/usr/lib/python3.6', '/usr/lib/python3.6/lib-dynload', '', '/home/HwHiAiUser/.local/lib/python3.6/site-packages', '/usr/local/lib/python3.6/dist-packages', '/usr/lib/python3/dist-packages', '/home/HwHiAiUser/.local/lib/python3.6/site-packages/IPython/extensions', '/tmp/tmpsugl3pwn', '/home/HwHiAiUser/wubo/paintfix_python/atlas_utils', '/home/HwHiAiUser/wubo/paintfix_python/atlas_utils', '/home/HwHiAiUser/wubo/paintfix_python/atlas_utils', '/home/HwHiAiUser/wubo/paintfix_python/atlas_utils']\n",
      "[Sample] init resource stage:\n"
     ]
    },
    {
     "ename": "Exception",
     "evalue": "acl.rt.set_device failed ret=100002",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mException\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-4-628a158b5dcd>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m    304\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    305\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0m__name__\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;34m'__main__'\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 306\u001b[0;31m     \u001b[0mmain\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    307\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m<ipython-input-4-628a158b5dcd>\u001b[0m in \u001b[0;36mmain\u001b[0;34m()\u001b[0m\n\u001b[1;32m    252\u001b[0m     \u001b[0;31m#acl  init\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    253\u001b[0m     \u001b[0macl_resource\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mAclResource\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 254\u001b[0;31m     \u001b[0mstream\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0macl_resource\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0minit\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    255\u001b[0m     \u001b[0;31m#deviceId = 0;\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    256\u001b[0m     \u001b[0;31m#acl.rt.set_device(deviceId)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/mhc/paintfix_python/acl_resource.py\u001b[0m in \u001b[0;36minit\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m     22\u001b[0m         \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"[Sample] init resource stage:\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     23\u001b[0m         \u001b[0mret\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0macl\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0minit\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 24\u001b[0;31m         \u001b[0mcheck_ret\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"acl.rt.set_device\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mret\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     25\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     26\u001b[0m         \u001b[0mret\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0macl\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrt\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mset_device\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdevice_id\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/mhc/paintfix_python/atlas_utils/utils.py\u001b[0m in \u001b[0;36mcheck_ret\u001b[0;34m(message, ret)\u001b[0m\n\u001b[1;32m      5\u001b[0m     \u001b[0;32mif\u001b[0m \u001b[0mret\u001b[0m \u001b[0;34m!=\u001b[0m \u001b[0mACL_ERROR_NONE\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      6\u001b[0m         raise Exception(\"{} failed ret={}\"\n\u001b[0;32m----> 7\u001b[0;31m                         .format(message, ret))\n\u001b[0m\u001b[1;32m      8\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      9\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mcopy_data_device_to_host\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdevice_data\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdata_size\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mException\u001b[0m: acl.rt.set_device failed ret=100002"
     ]
    }
   ],
   "source": [
    "import sys\n",
    "import os\n",
    "import numpy as np\n",
    "import acl\n",
    "import cv2\n",
    "from PIL import Image\n",
    "import glob\n",
    "import time\n",
    "\n",
    "atlas_path = \"/home/HwHiAiUser/wubo/paintfix_python/atlas_utils\"\n",
    "sys.path.append(atlas_path)\n",
    "print(sys.path)\n",
    "\n",
    "from atlas_utils.constants import *\n",
    "from acl_resource import AclResource\n",
    "from atlas_utils.utils import *\n",
    "from acl_model import Model\n",
    "from atlas_utils.acl_image import AclImage\n",
    "\n",
    "\n",
    "OUTPUT_DIR = './out/'\n",
    "MODEL_PATH = \"./model/hifill_34.om\"\n",
    "MODEL_MATMUL_PATH = \"./model/matmul_paint_3072.om\"\n",
    "#MODEL_MATMUL_PATH = \"./model/matmultst.om\"\n",
    "MODEL_WIDTH = 512\n",
    "MODEL_HEIGHT = 512\n",
    "\n",
    "INPUT_SIZE = 512  \n",
    "ATTENTION_SIZE = 32 \n",
    "MULTIPLE = 6\n",
    "\n",
    "NPTYPE_FLOAT32 = np.float32\n",
    "\n",
    "def sort(str_lst):\n",
    "    return [s for s in sorted(str_lst)]\n",
    "\n",
    "def resize_ave(img, MULTIPLE):\n",
    "    img = img.astype(NPTYPE_FLOAT32)\n",
    "    img_patches = extract_image_patches(img, MULTIPLE)\n",
    "    img = np.mean(img_patches, axis=(2,3))\n",
    "    return img\n",
    "def reconstruct_residual_from_patches(residual, multiple):\n",
    "    residual = np.reshape(residual, [ATTENTION_SIZE, ATTENTION_SIZE, multiple, multiple, 3])\n",
    "    residual = np.transpose(residual, [0,2,1,3,4])\n",
    "    return np.reshape(residual, [ATTENTION_SIZE * multiple, ATTENTION_SIZE * multiple, 3])\n",
    "\n",
    "# extract image patches\n",
    "def extract_image_patches(img, multiple):\n",
    "    h, w, c = img.shape\n",
    "    img = np.reshape(img, [h//multiple, multiple, w//multiple, multiple, c])\n",
    "    img = np.transpose(img, [0,2,1,3,4])\n",
    "    return img\n",
    "\n",
    "def pre_process(raw_img, raw_mask):\n",
    "    raw_mask = raw_mask.astype(NPTYPE_FLOAT32) / 255.\n",
    "    raw_img = raw_img.astype(NPTYPE_FLOAT32)\n",
    "\n",
    "    # resize raw image & mask to desinated size\n",
    "    large_img = cv2.resize(raw_img,  (MULTIPLE * INPUT_SIZE, MULTIPLE * INPUT_SIZE), interpolation = cv2. INTER_LINEAR)\n",
    "    large_mask = cv2.resize(raw_mask, (MULTIPLE * INPUT_SIZE, MULTIPLE * INPUT_SIZE), interpolation = cv2.INTER_NEAREST)\n",
    "    \n",
    "    # down-sample large image & mask to 512x512\n",
    "    small_img = resize_ave(large_img, MULTIPLE)\n",
    "    small_mask = cv2.resize(raw_mask, (INPUT_SIZE, INPUT_SIZE), interpolation = cv2.INTER_NEAREST)\n",
    "    \n",
    "    # set hole region to 1. and backgroun to 0.\n",
    "    small_mask = 1. - small_mask\n",
    "    return large_img, large_mask, small_img, small_mask\n",
    "\n",
    "def read_imgs_masks(images, masks):\n",
    "    paths_img = glob.glob(images + '/*.*[gG]')\n",
    "    paths_mask = glob.glob(masks + '/*.*[gG]')\n",
    "    paths_img = sort(paths_img)\n",
    "    paths_mask = sort(paths_mask)\n",
    "    print(paths_img)\n",
    "    print(paths_mask)\n",
    "    return paths_img, paths_mask\n",
    "    \n",
    "def matmul_ex(array_a, array_b, stream):\n",
    "    in_dtype, out_dtype = 1, 1\n",
    "    size_c = array_a.shape[0] * array_b.shape[1] * acl.data_type_size(float)\n",
    "\n",
    "    dev_matrix_a, ret = acl.rt.malloc(array_a.nbytes, ACL_MEM_MALLOC_NORMAL_ONLY)\n",
    "    dev_matrix_b, ret = acl.rt.malloc(array_b.nbytes, ACL_MEM_MALLOC_NORMAL_ONLY)\n",
    "    dev_matrix_c, ret = acl.rt.malloc(size_c, ACL_MEM_MALLOC_NORMAL_ONLY)\n",
    "\n",
    "    host_matrix_c, ret = acl.rt.malloc_host(size_c)\n",
    "    \n",
    "    array_a = np.ascontiguousarray(array_a)\n",
    "    host_matrix_a = acl.util.numpy_to_ptr(array_a)\n",
    "    \n",
    "    array_b = np.ascontiguousarray(array_b)\n",
    "    host_matrix_b = acl.util.numpy_to_ptr(array_b)\n",
    "    \n",
    "    ret = acl.rt.memcpy(dev_matrix_a, array_a.nbytes, host_matrix_a, array_a.nbytes, ACL_MEMCPY_HOST_TO_DEVICE)\n",
    "    ret = acl.rt.memcpy(dev_matrix_b, array_b.nbytes, host_matrix_b, array_b.nbytes, ACL_MEMCPY_HOST_TO_DEVICE)\n",
    "        \n",
    "    ret = acl.blas.gemm_ex(0, 0, 0, 1024, 3072, 1024,\n",
    "                    1, dev_matrix_a, 1024, float, dev_matrix_b, 3072, float,\n",
    "                    0, dev_matrix_c, 1024, float, 0,\n",
    "                    stream)\n",
    "                    \n",
    "    #check_ret(\"acl.mdl.execute\", ret)\n",
    "    ret = acl.rt.synchronize_stream(stream);   \n",
    "    \n",
    "    acl.rt.memcpy(host_matrix_c, size_c, dev_matrix_c, size_c, ACL_MEMCPY_DEVICE_TO_HOST);\n",
    "    \n",
    "    array_c = acl.util.ptr_to_numpy(host_matrix_c,\n",
    "                                         (array_a.shape[0], array_b.shape[1]),\n",
    "                                         NPTYPE_FLOAT16)\n",
    "    print(\"ACL output:\\n\", array_c.shape)\n",
    "    return array_c\n",
    "\n",
    "def matmul_om(matmul_model,attention,residual):\n",
    "    attention_reshape = attention.reshape(1024,1024)\n",
    "    residual_reshape = residual.reshape(1024,3072*9)\n",
    "    result = []\n",
    "    for i in range(9):\n",
    "        resi = residual_reshape[:,i*3072:(i+1)*3072]\n",
    "        #matmul_ret = matmul_ex(attention_reshape,resi)\n",
    "        #result.append(tmp.reshape(1024,3072))\n",
    "        matmul_ret = matmul_model.execute([attention_reshape,resi])   \n",
    "        tmp = matmul_ret[0]\n",
    "        result.append(tmp.reshape(1024,3072))\n",
    "    return np.hstack(result).reshape(ATTENTION_SIZE,ATTENTION_SIZE,3072*9)\n",
    "\n",
    "# residual aggregation module\n",
    "def residual_aggregate(model,residual, attention):\n",
    "    residual = extract_image_patches(residual, MULTIPLE * INPUT_SIZE//ATTENTION_SIZE)\n",
    "    residual = np.reshape(residual, [1, residual.shape[0] * residual.shape[1], -1])\n",
    "    residual = matmul_om(model,attention,residual)\n",
    "    #residual = np.matmul(attention, residual)\n",
    "    residual = reconstruct_residual_from_patches(residual, MULTIPLE * INPUT_SIZE//ATTENTION_SIZE)\n",
    "    return residual\n",
    "    \n",
    "\n",
    "def post_process(model,raw_img, large_img, large_mask, inpainted_512, img_512, mask_512, attention):\n",
    "    # compute the raw residual map\n",
    "    s = time.time()\n",
    "    h, w, c = raw_img.shape\n",
    "    low_base = cv2.resize(inpainted_512.astype(NPTYPE_FLOAT32), (INPUT_SIZE * MULTIPLE, INPUT_SIZE * MULTIPLE), interpolation = cv2.INTER_LINEAR) \n",
    "    low_large = cv2.resize(img_512.astype(NPTYPE_FLOAT32), (INPUT_SIZE * MULTIPLE, INPUT_SIZE * MULTIPLE), interpolation = cv2.INTER_LINEAR)\n",
    "    residual = (large_img - low_large) * large_mask\n",
    "    print('post_process before time', time.time() - s)\n",
    "    # reconstruct residual map using residual aggregation module\n",
    "    residual = residual_aggregate(model,residual, attention)\n",
    "    print('post_process residual_aggregate time', time.time() - s)\n",
    "    # compute large inpainted result\n",
    "    res_large = low_base + residual\n",
    "    res_large = np.clip(res_large, 0., 255.)\n",
    "\n",
    "    # resize large inpainted result to raw size\n",
    "    res_raw = cv2.resize(res_large, (w, h), interpolation = cv2.INTER_LINEAR)\n",
    "    \n",
    "    # paste the hole region to the original raw image\n",
    "    mask = cv2.resize(mask_512.astype(NPTYPE_FLOAT32), (w, h), interpolation = cv2.INTER_LINEAR)\n",
    "    mask = np.expand_dims(mask, axis=2)\n",
    "    \n",
    "    res_raw = res_raw * mask + raw_img * (1. - mask)\n",
    "    return res_raw.astype(np.uint8)\n",
    "   \n",
    "def matmul_test(array_a, array_b, stream):\n",
    "    in_dtype, out_dtype = 1, 1\n",
    "    size_c = array_a.shape[0] * array_b.shape[1] * acl.data_type_size(ACL_FLOAT)\n",
    "    #size_c = array_b.nbytes\n",
    "    print(\" size_c \", size_c)\n",
    "    dev_matrix_a, ret = acl.rt.malloc(array_a.nbytes, ACL_MEM_MALLOC_NORMAL_ONLY)\n",
    "    dev_matrix_b, ret = acl.rt.malloc(array_b.nbytes, ACL_MEM_MALLOC_NORMAL_ONLY)\n",
    "    dev_matrix_c, ret = acl.rt.malloc(size_c, ACL_MEM_MALLOC_NORMAL_ONLY)\n",
    "\n",
    "    host_matrix_c, ret = acl.rt.malloc_host(size_c)\n",
    "    \n",
    "    array_a = np.ascontiguousarray(array_a)\n",
    "    host_matrix_a = acl.util.numpy_to_ptr(array_a)\n",
    "    \n",
    "    array_b = np.ascontiguousarray(array_b)\n",
    "    host_matrix_b = acl.util.numpy_to_ptr(array_b)\n",
    "    \n",
    "    ret = acl.rt.memcpy(dev_matrix_a, array_a.nbytes, host_matrix_a, array_a.nbytes, ACL_MEMCPY_HOST_TO_DEVICE)\n",
    "    ret = acl.rt.memcpy(dev_matrix_b, array_b.nbytes, host_matrix_b, array_b.nbytes, ACL_MEMCPY_HOST_TO_DEVICE)\n",
    "    print(array_a.nbytes)  \n",
    "    print(array_b.nbytes)    \n",
    "    ret = acl.blas.gemm_ex(0, 0, 0, 2, 2, 2,\n",
    "                    1, dev_matrix_a, 2, in_dtype, dev_matrix_b, 2, in_dtype,\n",
    "                    0, dev_matrix_c, 2, out_dtype, 0,\n",
    "                    stream)\n",
    "                \n",
    "    #check_ret(\"acl.mdl.execute\", ret)\n",
    "    ret = acl.rt.synchronize_stream(stream);   \n",
    "    \n",
    "    acl.rt.memcpy(host_matrix_c, size_c, dev_matrix_c, size_c, ACL_MEMCPY_DEVICE_TO_HOST);\n",
    "    print(dev_matrix_c)\n",
    "    \n",
    "    array_c = acl.util.ptr_to_numpy(host_matrix_c,\n",
    "                                         (array_a.shape[0], array_b.shape[1]),\n",
    "                                         5)\n",
    "    print(\"ACL output:\\n\", array_c)\n",
    "    ret = acl.rt.free(dev_matrix_a)\n",
    "    ret = acl.rt.free(dev_matrix_b)\n",
    "    ret = acl.rt.free(dev_matrix_c)\n",
    "    return array_c\n",
    "    \n",
    "def matmul_test_200(array_a, array_b, stream):\n",
    "    in_dtype = 0\n",
    "    out_dtype = 0\n",
    "    size_c = array_a.shape[0] * array_b.shape[1] * acl.data_type_size(ACL_FLOAT)\n",
    "    #size_c = array_b.nbytes\n",
    "    print(\" size_c \", size_c)\n",
    "\n",
    "    dev_matrix_c, ret = acl.rt.malloc(size_c, ACL_MEM_MALLOC_NORMAL_ONLY)\n",
    "\n",
    "    print(ret)\n",
    "    array_a = np.ascontiguousarray(array_a)\n",
    "    \n",
    "    \n",
    "    dev_matrix_a = acl.util.numpy_to_ptr(array_a)\n",
    "    array_b = np.ascontiguousarray(array_b)\n",
    "    print(array_b)\n",
    "    dev_matrix_b = acl.util.numpy_to_ptr(array_b)\n",
    "    #print(dev_matrix_b[0],dev_matrix_b[1])\n",
    "    #output = acl.util.ptr_to_numpy(dev_matrix_a, (2,2), 11)\n",
    "    #print(output)\n",
    "    print(stream)\n",
    "    print(array_a.nbytes)  \n",
    "    print(array_b.nbytes)    \n",
    "    ret = acl.blas.gemm_ex(0, 0, 0, 2, 2, 2,\n",
    "                    1, dev_matrix_a, 2, in_dtype, dev_matrix_b, 2, in_dtype,\n",
    "                    0, dev_matrix_c, 2, out_dtype, 1,\n",
    "                    stream)\n",
    "                \n",
    "    print(ret)\n",
    "    ret = acl.rt.synchronize_stream(stream);   \n",
    "    \n",
    "    \n",
    "    array_c = acl.util.ptr_to_numpy(dev_matrix_c,\n",
    "                                         (array_a.shape[0], array_b.shape[1]),\n",
    "                                         11)\n",
    "    print(\"ACL output:\\n\", array_c)\n",
    " \n",
    "    return array_c\n",
    "  \n",
    "def main():\n",
    "\n",
    "    #read img/mask directory \n",
    "#     if (len(sys.argv) != 3):\n",
    "#         print(\"The App arg is invalid\")\n",
    "#         exit(1)\n",
    "\n",
    "    if not os.path.exists(OUTPUT_DIR):\n",
    "        os.mkdir(OUTPUT_DIR)\n",
    "\n",
    "    #acl  init\n",
    "    acl_resource = AclResource()\n",
    "    stream = acl_resource.init()\n",
    "    #deviceId = 0;\n",
    "    #acl.rt.set_device(deviceId)\n",
    "    #load model\n",
    "    model = Model(acl_resource,MODEL_PATH)\n",
    "    matmul_om = Model(acl_resource,MODEL_MATMUL_PATH)\n",
    "    '''\n",
    "    ret = acl.op.set_model_dir(MODEL_MATMUL_PATH)\n",
    "    a = np.array([[1,2],\n",
    "                 [3,4]],dtype=np.float32)\n",
    "    b = np.array([[5,6],\n",
    "                 [7,8]],dtype=np.float32)\n",
    "    #c=a.dot(b)\n",
    "    #print(c)\n",
    "    print(\"*\"*30)\n",
    "    ct = matmul_test_200(a, b, stream)\n",
    "    print(ct)\n",
    "    return \n",
    "    '''\n",
    "    image_dir = './data'\n",
    "    masks_dir = './mask'\n",
    "    paths_img, paths_mask = read_imgs_masks(image_dir, masks_dir)\n",
    "    for i in range(len(paths_img)):\n",
    "        print('==========')\n",
    "        s = time.time()\n",
    "        raw_img = cv2.imread(paths_img[i]) \n",
    "        raw_mask = cv2.imread(paths_mask[i])\n",
    "        \n",
    "        cv.imshow('raw_img', raw_img)\n",
    "        \n",
    "        \n",
    "        img_large, mask_large, img_512, mask_512 = pre_process(raw_img, raw_mask)\n",
    "\n",
    "        img_512_hwc = np.ascontiguousarray(img_512)\n",
    "        mask_512_hwc = mask_512[:,:,0:1]\n",
    "        mask_512_hwc = mask_512_hwc.transpose(2,0,1).copy()        \n",
    "        resultList  = model.execute([img_512_hwc, mask_512_hwc,])        \n",
    "        inpainted_512 = resultList[0]\n",
    "        inpainted_512_temp = np.squeeze(inpainted_512)        \n",
    "        attention = resultList[1]\n",
    "        mask_512_new = resultList[2] \n",
    "          \n",
    "\n",
    "        # post-processing\n",
    "        res_raw_size = post_process(matmul_om,raw_img, img_large, mask_large, inpainted_512[0], img_512, mask_512_new[0], attention[0])\n",
    "        filename = './out/outpaint_' + os.path.basename(paths_img[i])\n",
    "        cv2.imwrite(filename , res_raw_size)\n",
    "        print('processing time', time.time() - s)\n",
    "        \n",
    "    print(\"Execute end\")\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    main()\n",
    " \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['/home/HwHiAiUser/mhc/paintfix_python', '/home/HwHiAiUser/Ascend', '/home/HwHiAiUser/mhc/paintfix_python', '/usr/lib/python36.zip', '/usr/lib/python3.6', '/usr/lib/python3.6/lib-dynload', '/home/HwHiAiUser/.local/lib/python3.6/site-packages', '/usr/local/lib/python3.6/dist-packages', '/usr/lib/python3/dist-packages', '/home/HwHiAiUser/wubo/paintfix_python/atlas_utils']\n",
      "[Sample] init resource stage:\n",
      "Init resource success\n",
      "load model  ./model/hifill_34.om\n",
      "Init model resource\n",
      "[Model] create model output dataset:\n",
      "[Model] create model output dataset success\n",
      "[Model] class Model init resource stage success\n",
      "load model  ./model/matmul_paint_3072.om\n",
      "Init model resource\n",
      "[Model] create model output dataset:\n",
      "[Model] create model output dataset success\n",
      "[Model] class Model init resource stage success\n",
      "['./data/1.jpg', './data/3.jpg', './data/4.jpg']\n",
      "['./mask/1.jpg', './mask/3.jpg', './mask/4.jpg']\n",
      "==========\n",
      "acl.mdl.execute cost 0:00:00.109354, model_id=1\n",
      "post_process before time 0.12269973754882812\n",
      "acl.mdl.execute cost 0:00:00.003528, model_id=2\n",
      "acl.mdl.execute cost 0:00:00.002906, model_id=2\n",
      "acl.mdl.execute cost 0:00:00.002971, model_id=2\n",
      "acl.mdl.execute cost 0:00:00.002752, model_id=2\n",
      "acl.mdl.execute cost 0:00:00.002836, model_id=2\n",
      "acl.mdl.execute cost 0:00:00.002943, model_id=2\n",
      "acl.mdl.execute cost 0:00:00.002953, model_id=2\n",
      "acl.mdl.execute cost 0:00:00.002923, model_id=2\n",
      "acl.mdl.execute cost 0:00:00.002849, model_id=2\n",
      "post_process residual_aggregate time 0.3878483772277832\n",
      "processing time 2.7915170192718506\n",
      "==========\n",
      "acl.mdl.execute cost 0:00:00.108534, model_id=1\n",
      "post_process before time 0.12118291854858398\n",
      "acl.mdl.execute cost 0:00:00.002935, model_id=2\n",
      "acl.mdl.execute cost 0:00:00.002946, model_id=2\n",
      "acl.mdl.execute cost 0:00:00.002849, model_id=2\n",
      "acl.mdl.execute cost 0:00:00.002836, model_id=2\n",
      "acl.mdl.execute cost 0:00:00.003168, model_id=2\n",
      "acl.mdl.execute cost 0:00:00.002905, model_id=2\n",
      "acl.mdl.execute cost 0:00:00.002836, model_id=2\n",
      "acl.mdl.execute cost 0:00:00.003681, model_id=2\n",
      "acl.mdl.execute cost 0:00:00.002894, model_id=2\n",
      "post_process residual_aggregate time 0.38866639137268066\n",
      "processing time 2.0586836338043213\n",
      "==========\n",
      "acl.mdl.execute cost 0:00:00.108806, model_id=1\n",
      "post_process before time 0.12129449844360352\n",
      "acl.mdl.execute cost 0:00:00.002934, model_id=2\n",
      "acl.mdl.execute cost 0:00:00.002908, model_id=2\n",
      "acl.mdl.execute cost 0:00:00.002928, model_id=2\n",
      "acl.mdl.execute cost 0:00:00.002894, model_id=2\n",
      "acl.mdl.execute cost 0:00:00.002815, model_id=2\n",
      "acl.mdl.execute cost 0:00:00.002967, model_id=2\n",
      "acl.mdl.execute cost 0:00:00.002884, model_id=2\n",
      "acl.mdl.execute cost 0:00:00.002930, model_id=2\n",
      "acl.mdl.execute cost 0:00:00.003132, model_id=2\n",
      "post_process residual_aggregate time 0.38540005683898926\n",
      "processing time 2.7641234397888184\n",
      "Execute end\n",
      "Release acl resource,  2\n",
      "Start relase resource  0\n",
      "Model start release...\n",
      "Model release source success\n",
      "Start relase resource  1\n",
      "Model start release...\n",
      "Model release source success\n",
      "Release acl resource success\n"
     ]
    }
   ],
   "source": [
    "! python3 test3072.py ./data ./mask"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
